package com.example.myapplication;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;


import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {

    private List<MainBean.ProductsDTO> arrayList = new ArrayList<>();
    public MainAdapter(List<MainBean.ProductsDTO> arrayList) { this.arrayList = arrayList; }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MainBean.ProductsDTO bean = arrayList.get(position);

        String url = bean.getImgUrlList().toString().substring(1,bean.getImgUrlList().toString().indexOf(","));
        System.out.println("URL=" + url);
        holder.main_ImageView.setImageResource(R.drawable.button_shape);

        if (bean.getName().length()>=18) { holder.name_TextView.setText( bean.getName().substring(0,18) + ""); }
        else { holder.name_TextView.setText( bean.getName() + ""); }

        holder.price_TextView.setText("¥" + bean.getPrice().intValue()/100 +"."+ bean.getPrice().intValue()%100);
        holder.sell_TextView.setText("已售" + bean.getSellNum().toString());


        //动态控制控件的宽高
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) holder.main_ImageView.getLayoutParams();
        int width=(MyApp.get().getWidth()-MyApp.get().dp2Px(32))/2;
        layoutParams.height = width;
        holder.main_ImageView.setLayoutParams(layoutParams);


        //使用Glide加载网络图片
        Glide.with(MyApp.get()).load(bean.getImgUrlList().get(0)).into(holder.main_ImageView);


        RoundedCornersTransform transform = new RoundedCornersTransform(MyApp.get(),MyApp.get().dp2Px(8));
        transform.setNeedCorner(true,true,false,false);
        RequestOptions options = new RequestOptions().transform(transform);
        Glide.with(MyApp.get()).load(bean.getImgUrlList().get(0)).apply(options).into(holder.main_ImageView);




    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_main, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }



    static class ViewHolder extends RecyclerView.ViewHolder {

        private final RelativeLayout main_RelativeLayout;
        private final ImageView main_ImageView;

        private final AppCompatTextView name_TextView;//名字
        private final AppCompatTextView price_TextView;//价格
        private final AppCompatTextView sell_TextView;//销量


        public ViewHolder(@NonNull View view) {
            super(view);
            main_RelativeLayout = view.findViewById(R.id.main_RelativeLayout);
            main_ImageView = view.findViewById(R.id.main_ImageView);

            name_TextView = view.findViewById(R.id.name_TextView);//名字
            price_TextView = view.findViewById(R.id.price_TextView);//价格
            sell_TextView = view.findViewById(R.id.sell_TextView);//销量


        }
    }

}
